package com.starview.star.compiler.util;

import java.rmi.server.UID;
import java.util.HashMap;
import java.util.Map;

/**
 * Implement a gensym function that generates unique identifiers
 * 
 * @author fgm
 * 
 */
public class GenSym
{
  @SuppressWarnings("unused")
  private static final long varJvmId = Math.abs((long) new UID().hashCode());

  private static Map<String, Long> counters = new HashMap<String, Long>();

  public static String genSym(String prefix)
  {
    Long counter = counters.get(prefix);
    if (counter == null) {
      counter = 0L;
      counters.put(prefix, 1L);
    } else
      counters.put(prefix, counter + 1);
    return prefix + /*"_" + varJvmId +*/ "_" + counter;
  }

  public static long counter(String prefix)
  {
    Long counter = counters.get(prefix);

    if (counter == null)
      counter = 0l;

    counters.put(prefix, counter + 1);
    return counter;
  }

  public static String genSym()
  {
    return genSym("__");
  }
}
